package cn.yto.print.exception;

public class BluetoothCommException extends RuntimeException {

    private final String msg;
    protected Integer code;
    protected Object data;
    private Object[] args;

    public BluetoothCommException(Integer code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public BluetoothCommException(Integer code, String msg, Object... args) {
        super(msg);
        this.code = code;
        this.msg = msg;
        this.args = args;
    }

    public BluetoothCommException(Integer code, String msg, Throwable cause, Object... args) {
        super(msg, cause);
        this.code = code;
        this.msg = msg;
        this.args = args;
    }

    public BluetoothCommException(Integer code, Throwable cause) {
        super(cause);
        this.code = code;
        this.msg = cause != null ? cause.getMessage() : null;
    }

    public BluetoothCommException(String msg, Throwable cause) {
        super(cause);
        this.msg = msg;
    }

    public BluetoothCommException(String msg) {
        this.msg = msg;
    }

    public static BluetoothCommException of(Integer code, String msg) {
        return new BluetoothCommException(code, msg);
    }

    public static BluetoothCommException of(Integer code, Throwable cause) {
        return new BluetoothCommException(code, cause);
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Object[] getArgs() {
        return args;
    }

    public Object getData() {
        return data;
    }

    public BluetoothCommException setData(Object data) {
        this.data = data;
        return this;
    }

    @Override
    public String getMessage() {
        if (args == null || args.length == 0 || msg == null) {
            return msg;
        }
        try {
            return String.format(msg, args);
        } catch (Exception e) {
            return "Format error in message: " + msg;
        }
    }

    @Override
    public String toString() {
        return "BluetoothCommException{" +
                "code='" + code + '\'' +
                ", message='" + getMessage() + '\'' +
                ", data=" + data +
                '}';
    }

}
